package com_github_lib_base.view.rv

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding

// 布局类型构建器
// 修正后的ItemTypeBuilder类
class ItemTypeBuilder<T> {
     var matcher: (T) -> Boolean = { true }
     lateinit var layoutInflater: (LayoutInflater, ViewGroup) -> ViewDataBinding
     lateinit var binder: ItemBinder<T, ViewDataBinding>

     inline fun <reified VB : ViewDataBinding> match(
         noinline predicate: (T) -> Boolean,
         noinline inflate: (LayoutInflater, ViewGroup) -> VB = BindingMagic.createInflater(),
         crossinline bind: VB.(item: T, position: Int) -> Unit
    ) {
        matcher = predicate
        layoutInflater = inflate
        binder = object : ItemBinder<T, VB> {
            override fun bind(item: T, binding: VB, position: Int) {
                binding.bind(item,  position)
            }
        } as ItemBinder<T, ViewDataBinding>
    }

    fun build(): ItemType<T, ViewDataBinding> {
        return ItemType(matcher, layoutInflater, binder)
    }
}